package chixing.day13;

public class FinallyReturnDemo {
    public static void main(String[] args) {
//        System.out.println(fun1());
        System.out.println(fun2());
    }

    /**
     * try-catch-finally
     * 1.try-catch-finally 中有return先执行return，在执行finally
     * 2.在finally中修改了 返回值变量，返回值会不会改变？？？
     *      a.返回值类型时基本数据类型，本质是 值传递 ，返回值不会改变，参考fun1()
     *      b.返回值类型为引用数据类型，本质是 地址传递，返回值会改变，参考fun2()
     */
    public static int fun1(){
        int a = 10;
        try {
            System.out.println("---------try---------");
            return a;//int value【返回值】 = a ，将a的值 复制 了一份给 返回值 value
        }catch (Exception e){
            System.out.println("---------catch---------");
            return --a;
        }finally {
            a+=2;
            System.out.println("---------finally--------- a = " + a);
        }

    }
    public static Book fun2(){
        Book book = new Book("java",10);
        try {
            System.out.println("---------try---------");
            return book;//Book value【返回值】=book; 即book、value指向同一块内存地址
        }catch (Exception e){
            return null;
        }finally {
            System.out.println("---------finally---------");
            book.setBookName("Python");
            book.setBookPrice(77);
        }

    }


}
class Book {
    String bookName;
    int bookPrice;

    public Book(String bookName, int bookPrice) {
        this.bookName = bookName;
        this.bookPrice = bookPrice;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public float getBookPrice() {
        return bookPrice;
    }

    public void setBookPrice(int bookPrice) {
        this.bookPrice = bookPrice;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", bookPrice=" + bookPrice +
                '}';
    }
}
